package com.factory.test.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.face.sdk.yx5821.YX5821SdkServiceImpl;
import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityRs485Binding;
import com.factory.test.util.DataConversion;
import com.yecontech.sdk.base.io.IOCallBack;
import com.yecontech.sdk.base.io.RS485Manager;
import com.yecontech.sdk.base.io.WiegandManager;
import com.yecontech.sdk.base.vo.Constants;

import java.util.Timer;
import java.util.TimerTask;


/**
 * RS485测试
 */
public class RS485Activity extends BaseTestActivity {

    private ActivityRs485Binding binding;
    private RS485Manager rs485Manager;
    private WiegandManager wiegandManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_rs485);


        rs485Manager = new YX5821SdkServiceImpl(context).getRS485Manager();
        rs485Manager.open(new IOCallBack() {
            @Override
            public void onReceiverData(byte[] by) {
                appendMessage(DataConversion.toHexString(by, by.length));
            }
        });

        wiegandManager = new YX5821SdkServiceImpl(context).getWiegandManager();
//        wiegandManager.open(Constants.WiegandBit.WIEGAND26, new IOCallBack() {
//            @Override
//            public void onReceiverData(byte[] by) {
//                //appendMessage(DataConversion.toHexString(by, by.length));
//            }
//        });

        binding.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = binding.input.getText().toString();

                if(isEmpty(s)){
                    showWarning("请输入16进制数据");
                    return;
                }

                if(s.length() % 2 != 0){
                    showError("仅支持收发16进制");
                    return;
                }

                byte[] by = DataConversion.toByteArray(s);
                rs485Manager.send(by);
            }
        });

        binding.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.textView.setText("");
            }
        });

        myTask = new MyTask();
        Timer timer = new Timer();
        timer.schedule(myTask, 1000, 2000);
    }

    private MyTask myTask;
    private class MyTask extends TimerTask {
        @Override
        public void run() {
            wiegandManager.send(new byte[]{0x04, 0x03, 0x02, 0x01});
        }
    }

    public void appendMessage(String s){
        handler.post(new Runnable() {
            @Override
            public void run() {
                binding.textView.append(s);
                binding.textView.append("\n");
            }
        });

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 100);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        rs485Manager.close();
        myTask.cancel();
        wiegandManager.close();
    }
}
